<template>
	<view class="commodity">
		<!-- <uni-nav-bar title="商品管理" left-icon="left" @clickLeft="navBack" backgroundColor="#2CB9A4" color="#F8F8F8"></uni-nav-bar> -->
		<!-- <u-sticky style="background:#fff">
			<view class="tabs">
				<u-tabs :list="tabs" :scrollable='false' @click="selectTab" bg-color="#F5F5F5" lineColor="#2CB9A4"
					:current="tabIndex" @change="pickerChange" lineWidth="60" :activeStyle="{
			    color: '#2CB9A4',
			    transform: 'scale(1.05)'
			}"></u-tabs>
			</view>
		</u-sticky> -->
		<view class="add">
			<u-button class="custom-style" :text="form.type_data==='1'?'新增次卡':'新增商品'"
				customStyle="background: #fff ;border: 1rpx solid;color:#2CB9A4;border-radius: 20px;height: 30px;"
				@click="addCommodity">
			</u-button>
		</view>
		<view class="list f-column" v-for="(item,index) in list" :key="index">
			<view class="list_item" :style="{background:item.status==1?'#fff':'#fff'}">
				<view class="content">
					<view class="item" style="color: rgb(44, 185, 164);font-size: 26rpx;" v-if="form.type_data == 1">
						<text>有效期：</text><text>{{item.start_time}} - {{item.end_time}}</text>
					</view>
					<view class="item">
						<view class="icon">
							<image :src="item.images?item.images[0]:'../../static/commIcon.png'" mode="" class="image" mode="aspectFill">
							</image>
						</view>
						<view class="entityList">
							<view class="topName" style="font-size: 32rpx;">
								<text class="text">{{item.name}}</text>
								<view class="right" style="color: #2CB9A4;">
									￥{{item.price}}
								</view>
							</view>
							<view class="name">
								<text class="text">{{item.refer||''}}</text>
								<view class="right">
									<u-switch v-model="item.statusValue" activeColor="#2CB9A4" size="20" @change="changeSwitch($event,item)" ></u-switch>
								</view>
							</view>
						</view>
					</view>
					<view class="commodiyBot">
						<view class="botBtn">
							<u-button class="custom-style" text="删除"
								customStyle="background: #FF6666;color:#fff;border-radius: 20rpx;height: 30px;padding:0 30rpx;"
								@click="goDelete(item.id)">
							</u-button>
						</view>
						<view class="botBtn">
							<u-button class="custom-style" text="编辑"
								customStyle="background: #2CB9A4;color:#fff;border-radius: 20rpx;height: 30px;padding:0 30rpx;"
								@click="goEdit(item)">
							</u-button>
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="noMore" v-if="last_page==form.page&&list.length!=0"
			style="text-align: center;color: #797575;margin-top: 20rpx;padding-bottom: 60rpx;">
			数据加载完成...
		</view>
		<u-empty text="暂无列表哦~~" mode="list" v-if="list.length==0" margin-top='50'></u-empty>
		<u-modal :show="showDelete" :closeOnClickOverlay="true" :showCancelButton='true' :title="'提示'"
			@confirm='confirm' @cancel='showDelete=false' :content="delContent" confirmColor="#2CB9A4"></u-modal>
	</view>
</template>

<script>
	import {
		listGoods,
		delGoods,
		shelvesGoods
	} from '@/request/api.js'
	export default {
		data() {
			return {
				delContent:'是否删除该商品？',
				tabs: [{
					name: '全部'
				}, {
					name: '商品类'
				}, {
					name: '次卡类'
				}],
				tabIndex: 0,
				value1: true,
				showDelete: false,
				list: [],
				form: {
					page: 1,
					type_data: 2,//类型:0=普通商品,1=洗车,2=全部
				},
				last_page: 1,
				deleteId: '',
			};
		},
		onLoad(data) {
			this.form.type_data = data.type
			if(this.form.type_data == 0){
				uni.setNavigationBarTitle({
					title:'商品管理'
				})
			}else{
				uni.setNavigationBarTitle({
					title:'次卡管理'
				})
			}
			console.log(this.form.type_data,111)
			this.$forceUpdate()
			
		},
		onShow() {
			console.log(111)
			this.form.page = 1
			this.list = [] 
			this.getList()
		},
		onReachBottom() {
			// console.log(111)
			if (this.form.page < this.last_page) {
				this.form.page = this.form.page + 1
				this.getList()
			}
		},
		methods: {
			async changeSwitch(value,item){
				console.log(value,item)
				let data = {
					status: value ? 1 : 0,
					id: item.id,
				}
				let res  = await shelvesGoods(data)
				this.form.page = 1
				this.list = []
				this.getList()
				// uni.showToast({
				// 	title:'修改成功'
				// })
			},
			pickerChange(e) {
				let index = e.index;
				switch (index) {
					case 0:
						this.form.type_data = 2
						break;
					case 1:
						this.form.type_data = 0
						break;
					case 2:
						this.form.type_data = 1
						break;
				}
				this.form.page = 1
				this.list = []
				this.getList()
			},
			async confirm() {
				let res = await delGoods({
					id: this.deleteId
				})
				this.showDelete = false;
				uni.showToast({
					icon: 'none',
					title: '删除成功！'
				})
				this.form.page = 1
				this.list = []
				this.getList()
			},
			goDelete(e) {
				console.log(e, 'eeee')
				if(this.form.type_data ==0){
					this.delContent = '是否删除该商品？'
				}else{
					this.delContent = '是否删除该次卡？'
				}
				this.deleteId = e;
				this.showDelete = true;
			},
			async getList() {
				let res = await listGoods(this.form)
				this.list = this.list.concat(res.data.data)
				this.list.forEach(item=>{
					if(item.status == 1){
						item['statusValue'] = true
					}else{
						item['statusValue'] = false
					}
				})
				this.last_page = res.data.last_page
				console.log(res, '商品列表')
			},
			addCommodity() {
				uni.navigateTo({
					url: '/my_pages/addCommInfo/addCommInfo?type='+this.form.type_data
				})
			},
			goEdit(item) {
				uni.navigateTo({
					url: '/my_pages/addCommInfo/addCommInfo?data=' + JSON.stringify(item)+'&type='+this.form.type_data
				})
			}
		}
	}
</script>

<style lang="scss">
	::v-deep .u-tabs {
		background: #fff;
		margin-left: -15rpx;
		margin-right: -15rpx;
	}

	.commodity {
		font-size: 30rpx;
		padding: 0 20rpx 20rpx 20rpx;

		.add {
			width: 23%;
			    position: fixed;
			    top: 16rpx;
			    right: 20rpx;
			    z-index: 1000;

		}

		.list {
			margin-top: 20rpx;

			.content {
				background-color: #fff;
				// box-shadow: 0px 0px 10px rgba(8, 23, 56, 0.05);
				// border-radius: 10px;
				// padding: 20rpx;
				margin-top: 20rpx;

				.conBot {
					align-items: flex-end;

					.f-row {
						display: flex;
						align-items: flex-end;
					}

					.goNear {
						background: #2CB9A4;
						color: #fff;
						height: 60rpx;
						justify-content: center;
						align-items: center;
						border-radius: 10rpx;
						padding: 10rpx 20rpx;
						font-size: 32rpx;
						align-items: center;
					}

					display: flex;
					justify-content: space-between;
					color: #2CB9A4;

					.down {
						background: #C0EAE4;
						justify-content: center;
						display: flex;
						flex-direction: column;
						align-items: center;
						padding: 10rpx;
						font-size: 22rpx;
						border-radius: 10rpx;
						margin-right: 10rpx;
					}

					.down_price {
						margin-right: 10rpx;
					}

					.price {
						margin-right: 10rpx;
					}
				}
			}

			.list_item {
				display: flex;
				flex-direction: column;
				background: #2CB9A4;
				border-radius: 20rpx;
				padding: 20rpx;

				.title {
					color: #fff;
					font-size: 30rpx;
					font-weight: 600;
				}

				.item {
					display: flex;
					color: #505050;
					margin: 15rpx 0;
					border-bottom: 0.5px solid rgba(217, 217, 217, 0.3);
					padding-bottom: 15rpx;
					box-sizing: border-box;
					background: #fff;
					border-radius: 20rpx;

					.icon {
						width: 72rpx;
						height: 72rpx;
						width: 100rpx;
						height: 100rpx;

						.image {
							width: 100%;
							height: 100%;
						}
					}

					.entityList {
						flex: 1;
						display: flex;
						flex-direction: column;
						overflow: hidden;
						text-overflow: ellipsis;
						justify-content: space-between;

						.name {
							display: flex;
							justify-content: space-between;
							margin-left: 15rpx;
							white-space: nowrap;
							overflow: hidden;
							text-overflow: ellipsis;
							color: #BABABA;
							font-size: 26rpx;

							.text {
								width: 60%;
								overflow: hidden;
								text-overflow: ellipsis;
								white-space: nowrap;
							}

							.image {
								width: 26rpx;
								height: 26rpx;
								margin-right: 10rpx;
							}
						}

						.topName {
							display: flex;
							justify-content: space-between;
							margin-left: 15rpx;
							white-space: nowrap;
							overflow: hidden;
							text-overflow: ellipsis;
							color: #000;
							// font-weight: 600;
							font-size: 26rpx;

							.text {
								width: 60%;
								overflow: hidden;
								text-overflow: ellipsis;
								white-space: nowrap;
							}

							.image {
								width: 26rpx;
								height: 26rpx;
								margin-right: 10rpx;
							}
						}
					}
				}

				.commodiyBot {
					display: flex;
					flex-direction: row-reverse;

					.botBtn {
						margin-left: 30rpx;
					}
				}
			}
		}
	}
</style>